package com.example.lenovo.retrofit_test;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.lenovo.retrofit_test.bean.AppInfo;
import com.example.lenovo.retrofit_test.bean.PageBean;
import com.example.lenovo.retrofit_test.data.HttpUtils;
import com.example.lenovo.retrofit_test.exception.RxErrorHandler;
import com.example.lenovo.retrofit_test.exception.subscriber.ProgressDialogSubcriber;
import com.example.lenovo.retrofit_test.http.RxHttpReponseCompat;


/**
 * 主页面
 * @Author LXY
 * Created at 2017/7/27 11:41
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Context mContext;
    private Button mBt_http;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_view();
        init_listener();


    }

    private void init_view() {
        mContext=this;
        mBt_http = (Button) findViewById(R.id.bt_http);
    }



    private void init_listener() {
        mBt_http.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_http:
                // 访问网络,参数为json格式~
                HttpUtils.getApiService(MainActivity.this.getApplication()).getApps("{'page':0}")
                 .compose(RxHttpReponseCompat.<PageBean<AppInfo>>compatResult())
                 .subscribe(new ProgressDialogSubcriber<PageBean<AppInfo>>(new RxErrorHandler(mContext),mContext) {
                        @Override
                        public void onNext(PageBean<AppInfo> response) {
                            if (response != null) {
                                Toast.makeText(getApplication(),response.getDatas().get(1).toString(),Toast.LENGTH_SHORT).show();
                            } else {
                            }
                        }
                    });

                break;
        }
    }
}
